package com.sduept.nwld.dataserver.controller.mobiledata;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.springframework.beans.factory.annotation.Autowired;

import com.sduept.bigdata.document.entity.core.RecordAttachment;
import com.sduept.bigdata.document.services.RecordAttachmentManager;
import com.sduept.bigdata.document.core.CommonDocumentProperties;
import com.sduept.bigdata.web.core.manager.NoticeManager;
import com.sduept.bigdata.web.core.model.NoticeAnnouncement;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.utils.CharacterUtil;

/**
 *  首页通知公告界面
 */
@Named
@ViewScoped
public class NoticeView extends AbstractController{

	/**
	 *
	 */
	private static final long serialVersionUID = -7179147618278173951L;
	@Autowired
	private CommonDocumentProperties pros;
	@Autowired
	private NoticeManager nm;
	@Autowired
	private RecordAttachmentManager ram;

	private NoticeAnnouncement selectedNotice;
	private StreamedContent downloadedFile;//被下载的文件
	private List<RecordAttachment> attachments=new ArrayList<>();//附件
	@PostConstruct
	public void init() {
		String noticeId=getParameter("noticeId");
		selectedNotice=nm.getNoticeById(noticeId);
		if(null!=selectedNotice) {
			attachments=ram.getAttachmentsByOId(noticeId);
		}
	}
	public void downLoadFile(RecordAttachment ra) {
		InputStream ins=null;
		String fileName=null;
		if(null==ins) {
			try {
				ins = new FileInputStream(new File(
						pros.getDocumentPath()+ra.getPath()));
				fileName = CharacterUtil.urlEncode(ra.getDescription());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		downloadedFile=new DefaultStreamedContent(ins,
					"application/octet-stream",fileName);
		ra.setDownloadCount(ra.getDownloadCount()+1);
		ram.createOrUpdateRecordAttachment(ra);
	}

	public NoticeAnnouncement getSelectedNotice() {
		return selectedNotice;
	}
	public void setSelectedNotice(NoticeAnnouncement selectedNotice) {
		this.selectedNotice = selectedNotice;
	}
	public List<RecordAttachment> getAttachments() {
		return attachments;
	}
	public void setAttachments(List<RecordAttachment> attachments) {
		this.attachments = attachments;
	}
	public StreamedContent getDownloadedFile() {
		return downloadedFile;
	}
	public void setDownloadedFile(StreamedContent downloadedFile) {
		this.downloadedFile = downloadedFile;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

}
